package com.ruoyi.system.controller;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.CalcUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AlpElementCalc;
import com.ruoyi.system.domain.AlpMonthly;
import com.ruoyi.system.service.IAlpAuthorityService;
import com.ruoyi.system.service.IAlpCalcService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlpQuarterly;
import com.ruoyi.system.service.IAlpQuarterlyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 季度表Controller
 * 
 * @author zhangjun
 * @date 2020-02-21
 */
@Controller
@RequestMapping("/system/quarterly")
public class AlpQuarterlyController extends BaseController
{
    private String prefix = "system/quarterly";

    @Autowired
    private IAlpQuarterlyService alpQuarterlyService;

    @Autowired
    private IAlpCalcService alpCalcService;
    @Autowired
    private IAlpAuthorityService authorityFilter;

    @RequiresPermissions("system:quarterly:view")
    @GetMapping()
    public String quarterly()
    {
        return prefix + "/quarterlyList";
    }

    /**
     * 查询季度表列表
     */
    @RequiresPermissions("system:quarterly:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpQuarterly alpQuarterly)
    {
        startPage();
        List<AlpQuarterly> list = new ArrayList<>();

        HashMap<String,ArrayList<AlpElementCalc>> oneSalePrincipalMap = alpCalcService.getAllElementCalc();
        //迭代到 各个季度进行计算

        for (String key : oneSalePrincipalMap.keySet()) {
            AlpQuarterly quarterly = new AlpQuarterly();
            quarterly.setPrincipalName(key);
            ArrayList<AlpElementCalc> values = oneSalePrincipalMap.get(key);

            //计算各季度
            for (int i=1; i<= 4 ; i++) {
                BigDecimal resultBudget = new BigDecimal(0);
                BigDecimal resultPredict = new BigDecimal(0);
                BigDecimal resultActual = new BigDecimal(0);
                for (AlpElementCalc value : values) {
                    System.out.println("%%%%%%%%%%%"+ JSON.toJSONString(value));
                    if ((value.getMonth()/4+1) == i) {
                        resultBudget = resultBudget.add(value.getBudget());
                        resultPredict = resultPredict.add(value.getPredict());
                        resultActual = resultActual.add(value.getActual());
                    }
                }

                if (i == 1) {
                    quarterly.setQ1Budget(resultBudget);
                    quarterly.setQ1Predict(resultPredict);
                    quarterly.setQ1Actual(resultActual);
                    quarterly.setQ1FinishrateB(CalcUtil.calcRate(resultActual, resultBudget));
                    quarterly.setQ1FinishrateP(CalcUtil.calcRate(resultActual, resultPredict));
                    if (!CalcUtil.zero.equals(resultBudget)) {
                        quarterly.setQ1Remainsum(resultBudget.subtract(resultActual));
                    }
                }
                if (i == 2) {
                    quarterly.setQ2Budget(resultBudget);
                    quarterly.setQ2Predict(resultPredict);
                    quarterly.setQ2Actual(resultActual);
                    quarterly.setQ2FinishrateB(CalcUtil.calcRate(resultActual, resultBudget));
                    quarterly.setQ2FinishrateP(CalcUtil.calcRate(resultActual, resultPredict));
                    if (!CalcUtil.zero.equals(resultBudget)) {
                        quarterly.setQ2Remainsum(resultBudget.subtract(resultActual));
                    }
                }
                if (i == 3) {
                    quarterly.setQ3Budget(resultBudget);
                    quarterly.setQ3Predict(resultPredict);
                    quarterly.setQ3Actual(resultActual);
                    quarterly.setQ3FinishrateB(CalcUtil.calcRate(resultActual, resultBudget));
                    quarterly.setQ3FinishrateP(CalcUtil.calcRate(resultActual, resultPredict));
                    if (!CalcUtil.zero.equals(resultBudget)) {
                        quarterly.setQ3Remainsum(resultBudget.subtract(resultActual));
                    }
                }
                if (i == 4) {
                    quarterly.setQ4Budget(resultBudget);
                    quarterly.setQ4Predict(resultPredict);
                    quarterly.setQ4Actual(resultActual);
                    quarterly.setQ4FinishrateB(CalcUtil.calcRate(resultActual, resultBudget));
                    quarterly.setQ4FinishrateP(CalcUtil.calcRate(resultActual, resultPredict));
                    if (!CalcUtil.zero.equals(resultBudget)) {
                        quarterly.setQ4Remainsum(resultBudget.subtract(resultActual));
                    }
                }
            }

            list.add(quarterly);
            }
            System.out.println("%%%%%list%%%%%%"+JSON.toJSONString(list));

        return getDataTable(list);
    }

    /**
     * 导出季度表列表
     */
    @RequiresPermissions("system:quarterly:export")
    @Log(title = "季度表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpQuarterly alpQuarterly)
    {
        List<AlpQuarterly> list = alpQuarterlyService.selectAlpQuarterlyList(alpQuarterly);
        ExcelUtil<AlpQuarterly> util = new ExcelUtil<AlpQuarterly>(AlpQuarterly.class);
        return util.exportExcel(list, "quarterly");
    }

    /**
     * 新增季度表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存季度表
     */
    @RequiresPermissions("system:quarterly:add")
    @Log(title = "季度表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpQuarterly alpQuarterly)
    {
        return toAjax(alpQuarterlyService.insertAlpQuarterly(alpQuarterly));
    }

    /**
     * 修改季度表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AlpQuarterly alpQuarterly = alpQuarterlyService.selectAlpQuarterlyById(id);
        mmap.put("alpQuarterly", alpQuarterly);
        return prefix + "/edit";
    }

    /**
     * 修改保存季度表
     */
    @RequiresPermissions("system:quarterly:edit")
    @Log(title = "季度表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpQuarterly alpQuarterly)
    {
        return toAjax(alpQuarterlyService.updateAlpQuarterly(alpQuarterly));
    }

    /**
     * 删除季度表
     */
    @RequiresPermissions("system:quarterly:remove")
    @Log(title = "季度表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpQuarterlyService.deleteAlpQuarterlyByIds(ids));
    }


    /**
     * 季汇总公司级
     */
    @RequiresPermissions("system:quarterly:view")
    @PostMapping("/totalList")
    @ResponseBody
    public TableDataInfo totalList(){
        List<Map> list = alpQuarterlyService.getTotalQuarterlyList();
        return getDataTable(list);
    }

    /**
     * 季汇总部门级
     */
    @RequiresPermissions("system:quarterly:view")
    @PostMapping("/deptList")
    @ResponseBody
    public TableDataInfo deptList(String deptName){
        String deptNames = authorityFilter.authorityDeptFilter(deptName);
        if (StringUtils.isEmpty(deptNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpQuarterlyService.getDeptQuarterlyList(deptNames);
        return getDataTable(list);
    }

    /**
     * 季汇总个人级
     */
    @RequiresPermissions("system:quarterly:view")
    @PostMapping("/personalList")
    @ResponseBody
    public TableDataInfo personalList(String personName){
        String personNames = authorityFilter.authorityPersonFilter(personName);
        if (StringUtils.isEmpty(personNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpQuarterlyService.getPersonalQuarterlyList(personNames);
        return getDataTable(list);
    }
}
